﻿
<div class="ld-tabs">
    <ul>
        <li class="k-state-active">用户列表</li>
    </ul>
    <div>
        <div class="k-block divToolbar">
            <span class="k-textbox k-space-right">
                <input type="text" placeholder="请输入关键字" id="txtKey" />
                <a href="javascript:FilterUsers()" class="k-icon k-i-search">&nbsp;</a>
            </span>
            <button class="k-button" onclick="CreateNewUser()"><span class="k-icon k-i-plus"></span><span>添加新用户</span></button>
        </div>
        <div id="grid_user" style="text-align: center"></div>
    </div>
</div>
<script class="init">
    LocatedMenu("系统管理", "用户管理")
</script>
<script class="grid_user_columns">
    var userFields = [{
        title: "用户名",
        field: "UserName",
        width: 200
    }, {
        title: "姓名",
        field: "RealName",
        width: 100
    }, {
        title: "手机",
        field:"Mobile",
        width: 150
    }, {
        title: "角色",
        template:"#=ShowRoles(Roles)#",
        width: 150
    }, {
        title: "状态",
        template: "#=GeneralStatus(Status)#",
        width: 100
    }, {
        title: "操作",
        template: "#=GetOperations(ID)#",
        attributes: { style: "text-align:left" }
    }];

    function GetOperations(id) {
        var html = "<button class='k-button' onclick=\"EditUser(" + id + ")\"><span class='k-icon k-i-pencil'></span><span>修改</span></button>";
        html += "<button class='k-button' onclick=\"DeleteUser(" + id + ")\"><span class='k-icon k-i-close'></span><span>删除</span></button>";
        return html;
    }

    function EditUser(id) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "修改用户信息" });
            wndGeneralEdit.refresh({ url: "/Accounts/User/Edit/" + id });
            wndGeneralEdit.open();
        }
    }

    function DeleteUser(id) {
        if (confirm("真的要删除这个用户吗?")) {
            Whale.Remote.Operation({
                url: "/Accounts/User/Delete",
                parameters: { userID: id },
                success: function (result) {
                    if ($("#grid_user").length > 0)
                        $("#grid_user").data("kendoGrid").dataSource.read();
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    }

    function ShowRoles(roles) {
        var html = "";
        $(roles).each(function (i) {
            html += "," + roles[i].Description;
        });
        return html.substring(1);
    }

</script>

<script class="grid_config">
    $(function () {
        $("#grid_user").kendoGrid({
            columns: userFields,
            dataSource: Whale.dataSource({
                url: "/Accounts/User/Gets",
                getParameters: function () {
                    return {
                        key: $.trim($("#txtKey").val())
                    };
                },
                pageSize: 20

            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });

    function CreateNewUser() {
        wndGeneralEdit.setOptions({ title: "添加新的用户" });
        wndGeneralEdit.refresh({ url: "/Accounts/User/Edit" });
        wndGeneralEdit.open();
    }

    function FilterUsers() {
        $("#grid_user").data("kendoGrid").dataSource.page(0);
        $("#grid_user").data("kendoGrid").dataSource.read();
    }
</script>